package com.bitcola.security.admin.rest;

import com.bitcola.security.admin.biz.ColaUserBiz;
import com.bitcola.security.admin.biz.ColaWordOrderBiz;
import com.bitcola.security.admin.mapper.ColaMeWorkOrderMapper;
import com.bitcola.security.admin.mapper.ColaWorkOrderDetailMapper;
import com.bitcola.security.auth.client.i18n.ColaLanguage;
import com.bitcola.security.common.context.BaseContextHandler;
import com.bitcola.security.common.msg.AppResponse;
import com.bitcola.security.common.msg.TableResultResponse;
import com.bitcola.security.common.rest.BaseController;
import com.bitcola.security.common.util.AdminQuery;
import com.bitcola.entity.ColaMeWorkOrder;
import com.bitcola.entity.ColaMeWorkOrderDetail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wx
 * @create 2018-12-24 12:14
 **/
@RestController
@RequestMapping("cola/wordOrder")
public class ColaWorkOrderController extends BaseController<ColaWordOrderBiz, ColaMeWorkOrder> {

    @Autowired
    ColaUserBiz userBiz;

    @Autowired
    ColaWorkOrderDetailMapper detailMapper;

    @Autowired
    ColaMeWorkOrderMapper colaMeWorkOrderMapper;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("list")
    public TableResultResponse orderList(@RequestParam Map<String, Object> params) {
        AdminQuery query = new AdminQuery(params);
        String id = query.get("id").toString();
        String status = query.get("status").toString();
//      pageHelper分页
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<ColaMeWorkOrder> list = colaMeWorkOrderMapper.list(id, status);
        PageInfo<ColaMeWorkOrder> pageInfo = new PageInfo<>(list);
        return new TableResultResponse<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 完成
     *
     * @return
     */
    @RequestMapping(value = "complete", method = RequestMethod.POST)
    public AppResponse complete(@RequestBody ColaMeWorkOrder order) {
        if (StringUtils.isBlank(order.getId())) {
            return AppResponse.paramsError();
        }
        ColaMeWorkOrder colaMeWorkOrder = baseBiz.selectById(order.getId());
        colaMeWorkOrder.setStatus("Processed");
        baseBiz.updateById(colaMeWorkOrder);
        return AppResponse.ok();
    }

    /**
     * 回复
     *
     * @return
     */
    @RequestMapping(value = "reply", method = RequestMethod.POST)
    public AppResponse reply(@RequestBody ColaMeWorkOrderDetail detail) {
        if (StringUtils.isBlank(detail.getContent()) || StringUtils.isBlank(detail.getOrderId())) {
            return AppResponse.paramsError();
        }
        ColaMeWorkOrder colaMeWorkOrder = baseBiz.selectById(detail.getOrderId());
        if (colaMeWorkOrder.getStatus().equals("Processed")) {
            return AppResponse.error(ColaLanguage.get(ColaLanguage.ME_WORD_ORDER_COMPLETE));
        }
        detail.setId(UUID.randomUUID().toString());
        detail.setTime(System.currentTimeMillis());
        detail.setUserId(BaseContextHandler.getUserID());
        detail.setUsername(BaseContextHandler.getUsername());
        detailMapper.insertSelective(detail);
        if (colaMeWorkOrder.getStatus().equals("Unprocessed")) {
            colaMeWorkOrder.setStatus("Processing");
            baseBiz.updateById(colaMeWorkOrder);
        }
        return AppResponse.ok();
    }


}
